package com.digitalchina.lxnsy.toolkit;

import android.net.Uri;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.constant.Constant;
import com.digitalchina.lxnsy.constant.URL;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.toolkit
 * 创建人：白彦东
 * 创建时间：2017/1/3 17:27
 * 修改备注：
 * ================================================
 */

public class FrescoUtil {

    public static void showImageAndResize(String imageName, SimpleDraweeView simpleDraweeView, int width, int height) {


        Uri uri = Uri.parse(imageName);


        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setResizeOptions(new ResizeOptions(width, height))
                .build();
        PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                .setOldController(simpleDraweeView.getController())
                .setImageRequest(request)
                .build();
        simpleDraweeView.setController(controller);

    }

    /**
     * 直接传入图片完整路径
     *
     * @param imageURL
     * @param simpleDraweeView
     */
    public static void showImageForURL(String imageURL, SimpleDraweeView simpleDraweeView) {
        Uri uri = Uri.parse(imageURL);
        simpleDraweeView.setImageURI(uri);
    }

    /**
     * @param imagePath
     * @param simpleDraweeView
     */
    public static void showImageSmallForPath(String imagePath, SimpleDraweeView simpleDraweeView) {
        Uri uri = Uri.parse(imagePath);
        simpleDraweeView.setImageURI(uri);
    }

    /**
     * res目录下的资源
     *
     * @param res
     * @param simpleDraweeView
     */
    public static void showImageSmallForRes(int res, SimpleDraweeView simpleDraweeView) {
        Uri uri = Uri.parse("res://com.digitalchina.bigdata/" + res);
        simpleDraweeView.setImageURI(uri);
    }

    /**
     * app加载小图
     *
     * @param imageName        图片名称
     * @param simpleDraweeView 图片控件
     */
    public static void showImageSmall(String imageName, SimpleDraweeView simpleDraweeView) {
        if (StringUtil.isStrEmpty(imageName))
            imageName = "null";
        Uri uri = Uri.parse(URL.SERVER + imageName);
        simpleDraweeView.setImageURI(uri);
    }

    /**
     * app加载中图
     *
     * @param imageName        图片名称
     * @param simpleDraweeView 图片控件
     */
    public static void showImageMid(String imageName, SimpleDraweeView simpleDraweeView) {
        if (StringUtil.isStrEmpty(imageName))
            imageName = "null";
        Uri uri = Uri.parse(URL.SERVER + imageName);
        simpleDraweeView.setImageURI(uri);
    }

    /**
     * app加载大图
     *
     * @param imageName        图片名称
     * @param simpleDraweeView 图片控件
     */
    public static void showImageBig(String imageName, SimpleDraweeView simpleDraweeView) {
        if (StringUtil.isStrEmpty(imageName))
            imageName = "null";
        Uri uri = Uri.parse(URL.SERVER + imageName);
        simpleDraweeView.setImageURI(uri);
    }

    public static void showQRCode(String imageName, SimpleDraweeView simpleDraweeView) {
        Uri uri = Uri.parse(URL.SERVER + "FileService/showQRCode?moduleName=train&fileName=" + imageName);
        simpleDraweeView.setImageURI(uri);
    }


    /**
     * 加载农技智库列表图片
     *
     * @param imgUrl           图片路径
     * @param simpleDraweeView 图片控件
     */
    public static void showImageNoSize(String imgUrl, SimpleDraweeView simpleDraweeView) {
        Uri uri = Uri.parse(URL.SERVER + imgUrl.substring(1));
        simpleDraweeView.setImageURI(uri);
    }

    /**
     * 首页轮播图
     *
     * @param imgUrl
     * @param simpleDraweeView
     */
    public static void showImgForInfo(String imgUrl, SimpleDraweeView simpleDraweeView) {
        Uri uri = Uri.parse(URL.SERVER + imgUrl);
        simpleDraweeView.setImageURI(uri);
    }

    /**
     * 设置默认图
     * @param simpleDraweeView
     */
    public static void setDefaultImage( SimpleDraweeView simpleDraweeView) {
        Uri uri = Uri.parse("res://com.digitalchina.bigdata/" + R.drawable.bg_default);
        simpleDraweeView.setImageURI(uri);
    }
}
